/**$Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *   Copyright (C) 2008 - 2013 xingsheng.zhu@gmail.com All rights reserved.
 */
package com.blazemina.mina.config;

import com.blazemina.util.PropertyUtil;

/**
 * 根据{@link SocketConfigKeys#SOCKET_CONFIG_FILE}文件中的装配{@link SocketOption}
 * @author JackChu
 * @version 2.0.0 2013-2-20 上午10:19:29
 */
public class SocketOptionConfig {

	public SocketOption configSocketOpention() {
		PropertyUtil property = new PropertyUtil(
				SocketConfigKeys.SOCKET_CONFIG_FILE);
		String ioThread = property
				.getProperty(SocketConfigKeys.SOCKET_IO_THREAD);
		SocketOption option = new SocketOption();
		if (ioThread != null) {
			option.setIoThread(Integer.valueOf(ioThread));
		}
		String socketReceiveBuffer = property
				.getProperty(SocketConfigKeys.SOCKET_RECEIVE_BUFFER);
		if (socketReceiveBuffer != null) {
			option.setHasSocketReceiveBuffer(true);
			option.setSocketReceiveBuffer(Integer.valueOf(socketReceiveBuffer));
		}
		String socketSendBuffer = property
				.getProperty(SocketConfigKeys.SOCKET_SEND_BUFFER);
		if (socketSendBuffer != null) {
			option.setHasSocketSendBuffer(true);
			option.setSocketSendBuffer(Integer.valueOf(socketSendBuffer));
		}

		String keepAlive = property
				.getProperty(SocketConfigKeys.SOCKET_KEEP_ALIVE);
		if (keepAlive != null) {
			option.setHasKeepAlive(true);
			option.setKeepAlive(Boolean.valueOf(keepAlive));
		}

		String tcpNoDelay = property
				.getProperty(SocketConfigKeys.SOCKET_TCP_NO_DELAY);
		if (tcpNoDelay != null) {
			option.setHasTcpNoDelay(true);
			option.setTcpNoDelay(Boolean.valueOf(tcpNoDelay));
		}
		String bothIdleTimeSeconds = property
				.getProperty(SocketConfigKeys.SOCKET_BOTH_IDLE_TIME_SECONDS);
		if (bothIdleTimeSeconds != null) {
			option.setHasBothIdleTimeSeconds(true);
			option.setBothIdleTimeSeconds(Integer.valueOf(
					bothIdleTimeSeconds));
		}
		String readIdleTimeSeconds = property.getProperty(SocketConfigKeys.SOCKET_READ_IDLE_TIME_SECONDS);
		
		if(readIdleTimeSeconds != null){
			option.setHasReadIdleTimeSeconds(true);
			option.setReadIdleTimeSeconds(Integer.valueOf(readIdleTimeSeconds));
		}
		
		String writeIdleTimeSeconds = property.getProperty(SocketConfigKeys.SOCKET_WRITE_IDLE_TIME_SECONDS);
		if(writeIdleTimeSeconds != null){
			option.setHasWriteIdleTimeSeconds(true);
			option.setWriteIdleTimeSeconds(Integer.valueOf(writeIdleTimeSeconds));
		}
		
		String codecFactory = property.getProperty(SocketConfigKeys.SOCKET_CODEC_FACTORY);
		option.setCodecFactory(codecFactory);
		
		String host = property.getProperty(SocketConfigKeys.SOCKET_HOST);
		if(host != null){
			option.setHost(host);
		}
		String port = property.getProperty(SocketConfigKeys.SOCKET_PORT);
		if(port != null){
			option.setPort(Integer.valueOf(port));
		}
		
		int robotNums = property.getInt(SocketConfigKeys.ROBOT_NUMS);
		option.setRobotNums(robotNums);
		int interval = property.getInt(SocketConfigKeys.ROBOT_INTERVAL);
		option.setInterval(interval);
		return option;
	}

}
